/**
 * 武器
 */
class knife extends BaseComp{
    private img_knife:eui.Image;
    private knife_scale:eui.Image;
    public isScale:boolean = false;
    private property:number;
    constructor(data?)
    {
        super('knifeView', data);
    }

    public init()
    {
        if(this.data)
        {
            this.img_knife.source = 'knife_'+this.data.id;
        }
        else
        {
            let id = RoleCache.instance().curWeaponId;
            this.data = WeaponTpl.instance().getWeaponDataByWeaponId(id);
            this.img_knife.source = 'knife_'+id;
        }
        this.img_knife.anchorOffsetX = this.img_knife.width >> 1;
        this.img_knife.anchorOffsetY = this.img_knife.height >> 1;
        this.width = this.img_knife.texture.textureWidth;
        this.height = this.img_knife.texture.textureHeight;
        this.setAnchor();
        this.setProperty();
    }

    //设置刀的属性
    public setProperty()
    {
        if(this.data)
        {
            let property = this.data.property;
            this.property = property;
        }
    }

    public getProperty()
    {
        return this.property;
    }

    public scaleAnimation(call:Function,scope)
    {
        this.knife_scale.source = this.img_knife.source;
        this.knife_scale.visible = true;
        let count = 0;
        let knife = this.knife_scale;
        knife.anchorOffsetX = knife.width >> 1;
        knife.anchorOffsetY = knife.height >> 1;
        let self = this;
        let func = function(count)
        {
            egret.Tween.get(knife).to({
                scaleX:1.6,
                scaleY:1.6,
                alpha:0
            },300).set({
                scaleX:1,
                scaleY:1,
                alpha:1
            }).wait(100).call(()=>{
                if(count < 2)
                {
                    count++;
                    func(count);
                }
                else
                {
                    knife.visible = false;
                    self.isScale = true;
                    self.img_knife.scaleX = self.img_knife.scaleY = 1.3;
                    if(call)
                    {
                        call.call(scope);
                    }
                }
            })
        }
        func(count);
    }

    public reBackScaleState()
    {
        this.isScale = false;
        egret.Tween.get(this.img_knife).to({
            scaleX:1,
            scaleY:1
        },300);
        // this.img_knife.scaleX = this.img_knife.scaleY = 1;
    }

    public setUI(data)
    {
        if(data)
        {
            this.data = data;
            this.img_knife.source = 'knife_' + this.data.id;
            this.width = this.img_knife.texture.textureWidth;
            this.height = this.img_knife.texture.textureHeight;
            this.setAnchor();
            this.setProperty();
        }
    }

    public addEvent()
    {
        GameUtil.addEventListener(EventConst.CHANGE_WEAPON, this.changeWeapon, this);
    }

    public changeWeapon()
    {
        let curWeaponId = RoleCache.instance().curWeaponId;
        let data = WeaponTpl.instance().getWeaponDataByWeaponId(curWeaponId);
        this.setUI(data);
    }

    public get da()
    {
        return this.data;
    }

    public removeEvent()
    {
        GameUtil.removeEventListener(EventConst.CHANGE_WEAPON, this.changeWeapon, this);
    }

    public clear()
    {
        super.clear();
        this.isScale = null;
        this.property = null;
        EffectUtil.instance().StopEffect(this);
        EffectUtil.instance().StopEffect(this.knife_scale);
    }
}

window['knife'] = knife;